#ifndef HTMLGENERATOR_H
#define HTMLGENERATOR_H

#include "BasicConfig.h"
#include "config.h"
#include "DB_Manager.h"
#include "CalendarStructure.h"
#include "textprinter.h"
#include "strutturaicone.h"
#define IMM_DIMENSIONE_MASSIMA 300
#define ICONAATTRI_LARGO 20
#include "bestqtidy.h"
#include "ziphandle.h"
#include "QmyDom.h"


#include <QTextDocument>

static inline QPixmap im_frombase( const QString xml )
{
    QByteArray xcode;
    xcode.append(xml);
    QByteArray precode(QByteArray::fromBase64(xcode));
    QPixmap pix;
     if (pix.loadFromData(precode)) {
        return pix;
     } else {
        return QPixmap();
     }
    
}



static inline void savenspng( QPixmap   pixer , QString file )
{
    pixer.save(file,"PNG");
}





static inline QPixmap pdftumbi( QPixmap  pixer , int resize )
{
     QPixmap revrender = pixer.scaledToHeight(resize,Qt::SmoothTransformation);
     QRectF clip(1,1,revrender.width(),revrender.height());
     QPainter *p = new QPainter(&revrender);
     p->setRenderHint(QPainter::Antialiasing, true);
     QPen pen;
     pen.setStyle( Qt::SolidLine );
     pen.setWidth( 2 );
     pen.setColor( QColor(Qt::white) );
     p->setPen( pen); 
     p->drawRect(clip);
     p->end();
     return revrender;
}





static inline QPixmap checkwi( QPixmap pic ,  int maxwi )
{
    if (pic.width() > maxwi) {
        return pic.scaledToWidth(maxwi,Qt::SmoothTransformation);
    } else {
        return pic;
    }
}

static inline QPixmap checkhi( QPixmap pic ,  int maxhi )
{
    if (pic.height() > maxhi) {
        return pic.scaledToHeight(maxhi,Qt::SmoothTransformation);
    } else {
        return pic;
    }
}



static inline QPixmap createColorTool( QColor e  )
{
	  int lati = 14;
    QPixmap pixmap(lati,lati);
    pixmap.fill(e);
    QRect ret(0,0,lati,lati);
    QPainter painter(&pixmap);
	  painter.setPen(QPen(QBrush(Qt::darkBlue),1)); 
    painter.drawRect(ret);
		painter.end();
    return pixmap;
}

static inline QPixmap createNullPic( int wi , int hi )
{
    QPixmap pixmap(wi,hi);
    pixmap.fill(Qt::transparent);
    return pixmap;
}



static inline QString HData( QDateTime start )
{
    return start.toString("dd.MM.yy");
}



class HtmlGenerator : public QObject
{
     Q_OBJECT
//
public:
HtmlGenerator( DB_Manager * dbin , int db_id , int fontsize);
virtual ~HtmlGenerator();
inline QTextDocument *pdf( const QString la )  {
    if (enable_doc) {
    return Dpdf.value(la);
    } else {
      return new QTextDocument();
    }
}

inline QmyDom webdocument() { return doc; }

inline QMap<QString,QTextDocument*> PDoc() { return Dpdf; }
inline bool canwork() { return db->current().isOpen(); }
DB_Manager *db;
QStringList languages;
QString html;
/* handler data */
bool enable_doc;
QMap<QString, QString> qbase;  /* values from dbs */

QTextStream out;
QString ImageListStream;
/* nummer icon struct ico */
QList<IcoLine> icones;
QList<SPics> lipic;
QMap<QString,QStringList> transall;
QMap<QString,QString> qtrans;  /* current running language */
void LoadTrans( const int position );  /* laod from transall to qtrans current language */
//////////void append_pic ( const QString def , QPixmap pic  );  /* insert unique image name */
/* ################### save data ##########################*/
int id;
QTidy *cleaner;
bool isConnect;
int anno_adess0;
QString nummer;
QString POINT;
QString daticalendario;
void  fillImage( QTextDocument * doc );
TableCalendar  QCaliVuoto;
QString HandlerWebCode( QString html  );
/* iterate strings to all language */
int Tipo_Oggetto;
QString addo();
QString TitoloGrande();
QString letti();
QString calendario( bool web );
QString caratteristiche(bool web);
QString text();
QString CPAPaese( const QString  paeseid );
QString texter( QString value );
QString legenda_calendario();
QString Html_Ico( const int nrico  );
QString tabella_attributi( QString lista );
QString HandlerImagesWeb();
QString TAGIMAGEONE;

QString DBWordInsert( QString txtdb , const QString la );

int current_lang_pos;
double FattorePerFranco;
QSettings setter;
bool drawcalendar;
QString xp0;
QString xp1;
QString xpx;
QString xpR;
QString ImageUrlFirst;
QString datiicone;
/* iterate strings */

QDomElement root;
QmyDom doc;

/* language , htmldata */
QMap<QString,QString> Fdoc;
/* imagename , image SPics format title url pixmap*/
QMap<QString,SPics> Fimg;
/* html + image QTextDocument */
QMap<QString,QTextDocument*> Dpdf;
QMap<QString,QDomElement> wwwpage;

signals:
     void document_having(bool,int);
public slots:
   void realinit();
   void init();


};


#endif // HTMLGENERATOR_H
